package com.diligreat.interceptor; 

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import com.diligreat.domain.Admin;
import com.diligreat.util.Constants;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;

/**  
 * @Author Bruce 
 * @Date 2012-10-13
 * @Comments
 */
public class AuthorityInterceptor extends  MethodFilterInterceptor {
	private static final long serialVersionUID = 1L;
	private Logger logger;

	@Override
	protected String doIntercept(ActionInvocation invocation) throws Exception {
		logger = LoggerFactory.getLogger(invocation.getAction().getClass());
		logger.info("execute AuthorityInterceptor...");
		String result = null;
		ActionContext actionContext = invocation.getInvocationContext();
		Map<String, Object> session = actionContext.getSession(); 
		Admin user = (Admin) session.get(Constants.SESSION_KEY_OP);
		System.out.println(user);
		if(user == null || !StringUtils.hasText(user.getOpName())){
			result = Action.LOGIN;
		}else{
			System.out.println(user.getOpName());
			try {
				result = invocation.invoke();
			} catch (Exception e) {
				logger.error("",e);
			}
		}
		//System.out.println("result="+result);
		return result;
	}
	
	public static void main(String arsg[]){
		System.out.println(StringUtils.hasText(null));
	}

}
 